
import { type PaginationProps } from "@pureadmin/table";
import { ElMessage, FormInstance } from "element-plus";
import { ref, reactive, nextTick, watch, onMounted } from "vue";
import { NdataList2 } from "../component/data"
import {
  EnterpriseCreditResultDGaeeilstt,
  CreditScoreMeasurement,
  GetAllByPagedAndSort,
} from "@/api/stu/DataScene";
import { useRoute } from "vue-router";
import qs from "qs";
export function useCreditReport() {
  const route = useRoute();
  const ScoreParticulars_ = ref();
  const dataList = ref<any>([]);
  const loading = ref<boolean>(true);
  const btnloading = ref<boolean>(false);
  const CreditVisible = ref<boolean>(false);
  const regex = /^[\u4e00-\u9fa5]{2,4}$/;// 校验2-4个字中文
  const regex2 = /^[\u4e00-\u9fa5]{2,18}$/;// 校验2-4个字中文
  const regPhone = /^1[3456789]\d{9}$/;
  const regID = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 20,
    currentPage: 1,
    pageSizes: [10, 20, 50, 100],
    background: true
  });
  const columns: TableColumnList = [
    {
      label: "序号",
      type: "index",
      minWidth: 100
    },
    {
      label: "企业名称",
      prop: "enterpriseName",
      minWidth: 100
    },
    {
      label: "企业法定代表人姓名",
      prop: "enterpriseLegaler",
      minWidth: 100
    },
    {
      label: "企业法人身份证号码",
      prop: "enterpriseLegalIDCard",
      minWidth: 100
    },
    {
      label: "评分模型",
      prop: "modelName",
      minWidth: 100
    },
    {
      label: "信用分",
      prop: "creditScore",
      minWidth: 100
    },
    {
      label: "信用等级",
      prop: "creditRating",
      minWidth: 100
    }, {
      label: "操作",
      fixed: "right",
      width: 160,
      slot: "operation"
    }
  ];

  const CreditScoreFrom = reactive({
    EnterpriseName: "", //企业名称
    legalName: "",//企业法定代表人名称
    legalId: "" //企业法人身份证号码
  })

  const rule = {
    EnterpriseName: [
      {
        required: true,
        validator: (rule: any, value: any, callback: () => void) => {
          if (regex2.test(CreditScoreFrom.EnterpriseName) === false) {
            return callback(new Error('请输2-18的中文名称'))
          } else {
            // 校验通过
            return callback()
          }
        },
        trigger: ["blur", "change"],
      }
    ],
    legalName: [
      {
        required: true,
        validator: (rule: any, value: any, callback: () => void) => {
          if (regex.test(CreditScoreFrom.legalName) === false) {
            return callback(new Error('请输2-4的中文名称'))
          } else {
            // 校验通过
            return callback()
          }
        },
        trigger: ["blur", "change"],
      }
    ],
    legalId: [
      {
        required: true,
        trigger: ["blur", "change"],
        validator: (rule: any, value: any, callback: () => void) => {
          if (regID.test(CreditScoreFrom.legalId) === false) {
            return callback(new Error('请输入正确的证件码'))
          } else {
            // 校验通过
            return callback()
          }
        },
      }
    ],
  }

  // 征信报告列表

  // 申请测算信用评分
  function ApplyCreditScore() {
    CreditVisible.value = true;
  }

  // 查看信用分详情
  async function ShowReportDestail(row) {
    const data = qs.stringify({
      enterpriseId: row.enterpriseId,
      modelId: row.modelId
    });
    const res = await EnterpriseCreditResultDGaeeilstt(data)
    if (res.success) {
      const data = {
        data: res.data,
        grade: row.creditRating,
        score: row.creditScore
      }
      ScoreParticulars_.value.ViewScore(data)
    } else {
      ElMessage.error(res.msg)
    }
    // console.log("企业-查看信用分详情", row)
    // ScoreParticulars_.value.ViewScore(NdataList2)
  }
  // 取消关闭
  function CloseVisible(row: FormInstance | undefined) {
    CreditVisible.value = false;
    row.resetFields();
  };

  // 确定
  function SubmitVisible(row: FormInstance | undefined) {
    (row as any).validate(async valid => {
      if (!valid) return;
      const { EnterpriseName, legalId, legalName } = CreditScoreFrom
      const data = {
        enterpriseName: EnterpriseName,
        enterpriseLegaler: legalName,
        enterpriseLegalIDCard: legalId,
        planId: route.query.planId,
        taskId: route.query.taskId,
        ecmId: "001"
      }
      btnloading.value = true;
      const res = await CreditScoreMeasurement(data);
      if (res.success) {
        ElMessage.success(res.msg)
        row.resetFields();
        btnloading.value = false;
        CreditVisible.value = false;
        GetAllEnterprisereportInfos()
      } else {
        setTimeout(() => { btnloading.value = false; }, 1500)
        ElMessage.error(res.msg)
      }
    })
  }

  function handleSizeChange() {
    GetAllEnterprisereportInfos()
  }

  function handleCurrentChange() {
    GetAllEnterprisereportInfos()
  }


  // 获取信用评分数据
  async function GetAllEnterprisereportInfos() {
    const data = {
      planId: route.query.planId,
      taskId: route.query.taskId,
      "pageIndex": pagination.currentPage,
      "pageCount": pagination.pageSize
    }
    const res = await GetAllByPagedAndSort(data);
    if (res.success) {
      pagination.total = res.data.totalCount;
      dataList.value = res.data.items;
      loading.value = false;
    } else {
      ElMessage.error(res.msg);
      setTimeout(() => {
        loading.value = false;
      }, 1500)
    }
  }

  onMounted(() => {
    GetAllEnterprisereportInfos()
  })

  return {
    rule,
    dataList,
    loading,
    CreditVisible,
    pagination,
    columns,
    btnloading,
    CreditScoreFrom,
    ScoreParticulars_,
    handleCurrentChange,
    handleSizeChange,
    ShowReportDestail,
    ApplyCreditScore,
    SubmitVisible,
    CloseVisible
  }
}
